package com.kexilo.core.common.validation;

import com.kexilo.core.common.annotation.Email;
import com.kexilo.core.common.utils.StringUtils;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * 邮箱验证器
 * 
 * @author Kexilo
 */
public class EmailValidator implements ConstraintValidator<Email, String> {
    
    /**
     * 邮箱正则表达式
     */
    private static final String EMAIL_PATTERN = 
        "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
    
    @Override
    public void initialize(Email constraintAnnotation) {
        // 初始化方法，可以获取注解的属性
    }
    
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // 空值由@NotNull或@NotBlank来验证
        if (StringUtils.isEmpty(value)) {
            return true;
        }
        
        return value.matches(EMAIL_PATTERN);
    }
}
